package com.yejiali.backend.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.yejiali.backend.common.httpcode.HttpCodeEnum;
import com.yejiali.backend.entity.vo.IdCardVO;
import com.yejiali.backend.entity.vo.SocialCardVO;
import com.yejiali.backend.response.Response;
import com.yejiali.backend.service.OCRService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author:yejiali
 * @Date:2025/5/28-05-28-8:49
 * @Decsription:com.yejiali.backend.service.impl
 * @Version:1.0
 */
@Service
public class OCRServiceImpl implements OCRService {

    @Autowired
    private Client client;

    /**
     * 身份证照识别信息
     * @return
     */
    @Override
    public Response idCard(MultipartFile file) throws IOException {
        //转换为文件流
        InputStream bodyStream = file.getInputStream();
        //文件流置入请求
        RecognizeIdcardRequest recognizeIdcardRequest = new RecognizeIdcardRequest()
                .setBody(bodyStream);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            RecognizeIdcardResponse response = client.recognizeIdcardWithOptions(recognizeIdcardRequest, runtime);
            RecognizeIdcardResponseBody body = response.getBody();
            String data = body.getData();
            JSONObject jsonObject = JSON.parseObject(data);
            //解析身份证信息
            JSONObject faceData = jsonObject.getJSONObject("data")
                    .getJSONObject("face")
                    .getJSONObject("data");
            IdCardVO cardVO=new IdCardVO();
            cardVO.setName(faceData.getString("name"));
            cardVO.setAddress(faceData.getString("address"));
            cardVO.setBirthday(faceData.getString("birthDate"));
            cardVO.setSex(faceData.getString("sex"));
            cardVO.setId(faceData.getString("idNumber"));
            cardVO.setNation(faceData.getString("ethnicity"));
            return Response.success(cardVO,"证件识别成功");
        } catch (Exception error) {
            System.out.println(error.getMessage());
        }
        return Response.error(HttpCodeEnum.CARD_RECOGNITION_FAILED.getCode(),"证件识别失败");
    }

    /**
     * 社保卡号识别
     * @param file
     * @return
     */
    @Override
    public Response socialCard(MultipartFile file) throws IOException {
        //转换为文件流
        InputStream bodyStream = file.getInputStream();
        RecognizeSocialSecurityCardVersionIIRequest recognizeSocialSecurityCardVersionIIRequest = new RecognizeSocialSecurityCardVersionIIRequest();
        recognizeSocialSecurityCardVersionIIRequest.setBody(bodyStream);
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            RecognizeSocialSecurityCardVersionIIResponse recognizeSocialSecurityCardVersionIIResponse = client.recognizeSocialSecurityCardVersionIIWithOptions(recognizeSocialSecurityCardVersionIIRequest, runtime);
            String data = recognizeSocialSecurityCardVersionIIResponse.getBody().getData();
            JSONObject jsonObject = JSON.parseObject(data);
            JSONObject socialInfo = jsonObject.getJSONObject("data");
            SocialCardVO socialCardVO=new SocialCardVO();
            socialCardVO.setSocialCard(socialInfo.getString("cardNumber"));
            socialCardVO.setName(socialInfo.getString("name"));
            socialCardVO.setBankAccount(socialInfo.getString("bankAccount"));
            return Response.success(socialCardVO,"证件识别成功");
        } catch (Exception error) {
            System.out.println(error.getMessage());
        }
        return Response.error(HttpCodeEnum.CARD_RECOGNITION_FAILED.getCode(),"证件识别失败");

    }



}
